package com.su.dolphin.core;

import android.os.Handler;
import android.os.Looper;

import com.su.dolphin.utils.LogUtil;

public final class AppThread {
	private final static int DEFAULT_DELAY = 0;
	private static android.os.Handler sUIHandler;
	private static android.os.Handler sDBHandler;
	private static android.os.Handler sIRHandler;
	private static android.os.Handler sTrkHandler;
	private static boolean isAppThreadStarted;

	public static void init() {
		if (isAppThreadStarted)
			return;
		isAppThreadStarted = true;

		// final android.os.HandlerThread dbThread = new
		// android.os.HandlerThread("dbThread",
		// android.os.Process.THREAD_PRIORITY_BACKGROUND);
		// dbThread.start();
		// final android.os.HandlerThread trkThread = new
		// android.os.HandlerThread("tr",
		// android.os.Process.THREAD_PRIORITY_BACKGROUND);
		// trkThread.start();
		// final android.os.HandlerThread irThread = new
		// android.os.HandlerThread("ir");
		// irThread.start();
		//
		sUIHandler = new android.os.Handler(android.os.Looper.getMainLooper());
		// sIRHandler = new android.os.Handler(irThread.getLooper());
		// sDBHandler = new android.os.Handler(dbThread.getLooper());
		// sTrkHandler = new android.os.Handler(trkThread.getLooper());
	}

	public static void stop() {
		if (isAppThreadStarted) {
			if (null != sDBHandler)
				sDBHandler.getLooper().quit();
			if (null != sIRHandler)
				sIRHandler.getLooper().quit();
			if (null != sTrkHandler)
				sTrkHandler.getLooper().quit();
			sUIHandler = null;
			sDBHandler = null;
			sIRHandler = null;
			sTrkHandler = null;
			isAppThreadStarted = false;
		}
	}

	public static boolean uiThreadCheck() {
		return Looper.getMainLooper().getThread() == Thread.currentThread();
	}

	public final static void uiRemoveCallback(Runnable runnable) {
		if (sUIHandler == null) {
			return;
		}
		sUIHandler.removeCallbacks(runnable);
	}

	public final static void dbPost(final Runnable run) {
		dbPost("DB Thread posting..", run, DEFAULT_DELAY);
	}

	public final static void uiPost(final Runnable run) {
		uiPost("UI Thread posting..", run, DEFAULT_DELAY);
	}

	public final static void irPost(final Runnable run) {
		irPost("IR Thread posting..", run, DEFAULT_DELAY);
	}

	public final static void trkPost(final Runnable run) {
		trkPost("TRK Thread posting..", run, DEFAULT_DELAY);
	}

	public final static void uiPost(final Runnable run, final long delay) {
		post(sUIHandler, "", run, delay);
	}

	public final static void uiPost(final String msg, final Runnable run,
			final long delay) {
		post(sUIHandler, msg, run, delay);
	}

	public final static void dbPost(final String msg, final Runnable run,
			final long delay) {
		post(sDBHandler, msg, run, delay);
	}

	public final static void irPost(final String msg, final Runnable run,
			final long delay) {
		post(sIRHandler, msg, run, delay);
	}

	public final static void trkPost(final String msg, final Runnable run,
			final long delay) {
		post(sTrkHandler, msg, run, delay);
	}

	private final static void post(Handler handler, final String msg,
			final Runnable run, final long delay) {
		if (handler == null) {
			LogUtil.e("handler is null");
		}
		final Runnable runner = new Runnable() {
			public void run() {
				final long startTime = System.nanoTime();
				try {
					run.run();
				} catch (Throwable ex) {
					LogUtil.e(ex.toString());
				} finally {
					LogUtil.d((null == msg ? "" : msg) + " ("
							+ (System.nanoTime() - startTime) / 1000000 + "ms)");
				}
			}
		};
		handler.postDelayed(runner, delay);
	};

}
